package np.com.abhishek.scrambler.utils;

import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import np.com.abhishek.scrambler.ui.Initializer;
import np.com.abhishek.scrambler.ui.SizeSelector;

/*
 * JFontChooser.java
 *
 * Created on April 18, 2006, 1:30 PM
 * is used to select the required font
 * this is my version of the font dialog box in java and hence is under my patent
 */

/**
 * 
 * @author Abhishek Shrestha
 */

public class JFontChooser extends JFrame implements ListSelectionListener,
		ActionListener {

	private static final long serialVersionUID = 4L;

	String fontList[], fontType[], fontSizes[], defaultTitle;

	JList lst1, lst2, lst3;

	JLabel lbl1, lbl2, lbl3, lbl4;

	JButton btn1, btn2;

	JTextField txt1, txt2, txt3;

	public static Font retFont;

	public Font font = new Font("Georgia", Font.BOLD, 100 / SizeSelector.size);

	public static boolean okPressed;

	public static ActionEvent e;

	public JFontChooser() {
		okPressed = false;
		lbl1 = new JLabel("Font:");
		lbl2 = new JLabel("Style:");
		lbl3 = new JLabel("Size:");
		lbl4 = new JLabel("AaBbYyZz");
		lbl4.setBorder(new EtchedBorder());
		lbl4.setHorizontalAlignment(SwingConstants.CENTER);

		lbl1.setBounds(10, 5, 50, 20);
		lbl2.setBounds(190, 5, 50, 20);
		lbl3.setBounds(295, 5, 50, 20);
		lbl4.setBounds(10, 25, 140, 55);

		txt1 = new JTextField(font.getName());
		txt2 = new JTextField("Regular");
		txt3 = new JTextField("" + font.getSize());

		txt1.setBounds(10, 30, 175, 20);
		txt2.setBounds(190, 30, 100, 20);
		txt3.setBounds(295, 30, 50, 20);

		JPanel pnl = new JPanel();
		pnl.setBorder(new TitledBorder("Sample"));
		pnl.add(lbl4);
		pnl.setBounds(350, 50, 160, 95);
		pnl.setLayout(null);

		btn1 = new JButton("OK");
		btn2 = new JButton("Cancel");

		btn1.setBounds(350, 155, 75, 25);
		btn2.setBounds(435, 155, 75, 25);

		String fontList[] = GraphicsEnvironment.getLocalGraphicsEnvironment()
				.getAvailableFontFamilyNames();
		String fontType[] = { "Regular", "Bold", "Italic", "Bold Italic" };
		String fontSizes[] = { "8", "9", "10", "11", "12", "14", "16", "18",
				"20", "22", "24", "26", "28", "36", "72" };

		lst1 = new JList(fontList);
		lst2 = new JList(fontType);
		lst3 = new JList(fontSizes);

		JScrollPane scrl1 = new JScrollPane(lst1); // holds the list of fonts
		JScrollPane scrl2 = new JScrollPane(lst2); // holds the attribute of
		// the font i.e. bold
		// italic...
		JScrollPane scrl3 = new JScrollPane(lst3); // holds ths size of the
		// font

		scrl1.setBounds(10, 55, 175, 130);
		scrl2.setBounds(190, 55, 100, 130);
		scrl3.setBounds(295, 55, 50, 130);

		btn1.addActionListener(this);
		btn2.addActionListener(this);

		lst1.addListSelectionListener(this);
		lst2.addListSelectionListener(this);
		lst3.addListSelectionListener(this);

		add(lbl1);
		add(lbl2);
		add(lbl3);

		add(txt1);
		add(txt2);
		add(txt3);
		add(pnl);

		add(scrl1);
		add(scrl2);
		add(scrl3);

		add(btn1);
		add(btn2);

		lbl4.setFont(font);

		getRootPane().setDefaultButton(btn1);
		setTitle(" - JFontChooser (c) 2006 Abhishek Shrestha BIM V(03/01) -");
		setSize(525, 225);
		setLayout(null);
		setResizable(false);
		setLocation(300, 300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btn1) {
			retFont = font;
			/*
			 * once the font is selected and OK button is pressed then each of
			 * the buttons in the board is to be changed according the selection
			 * made in the dialog box
			 */
			for (int m = 0; m < Initializer.tiles; m++)
				Initializer.btn[m].setFont(retFont);
		}
		// after updating all the buttons of course one no longer needs the
		// dialog box
		dispose();
	}

	public void valueChanged(ListSelectionEvent e) {
		String name = "Georgia", fontStyle, fontSize;

		int style = Font.BOLD, size = 100 / Initializer.size;

		if (e.getSource() == lst1) {
			name = lst1.getSelectedValue().toString();
			txt1.setText(name);
			setFontName(name);
		}
		if (e.getSource() == lst2) {
			fontStyle = lst2.getSelectedValue().toString();
			txt2.setText(fontStyle);

			if (fontStyle.equals("Bold"))
				style = Font.BOLD;
			else if (fontStyle.equals("Italic"))
				style = Font.ITALIC;
			else if (fontStyle.equals("Bold Italic"))
				style = Font.ITALIC | Font.BOLD;
			else if (fontStyle.equals("Regular"))
				style = Font.PLAIN;

			setFontStyle(style);
		}
		if (e.getSource() == lst3) {
			fontSize = lst3.getSelectedValue().toString();
			txt3.setText(fontSize);

			size = Integer.parseInt(fontSize);
			setFontSize(size);
		}
		lbl4.setFont(font);
	}

	public Font getFontInfo() {
		return font;
	}

	public void setFont(Font font) {
		this.font = font;
	}

	public String getFontName() {
		return font.getName();
	}

	public int getFontSize() {
		return font.getSize();
	}

	public int getFontStyle() {
		return font.getStyle();
	}

	public void setFontSize(int size) {
		font = new Font(font.getName(), font.getStyle(), size);
	}

	public void setFontName(String name) {
		font = new Font(name, font.getStyle(), font.getSize());
	}

	public void setFontStyle(int style) {
		font = new Font(font.getName(), style, font.getSize());
	}
}
